<template>
  <div class='container'>
    <div class="body">
      <div class="head">
        <div>
          <span>关键字<el-input class="keywords" type="text" v-model="per.keyword" placeholder="请输入内容"></el-input></span>
          <span>
            状态<el-select class="keywords" v-model="per.state" slot="prepend" placeholder="请选择">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </span>
          <el-button type="primary" @click="clear">清除</el-button>
          <el-button type="primary" @click="getList">搜索</el-button>
        </div>
        <div class="right">
          <el-button-group>
            <el-button class="depressed" type="primary" icon="el-icon-edit" @click="unpack">新增技巧</el-button>
          </el-button-group>
        </div>
      </div>
      <el-alert :title="'数据一共' + total + '条'" type="info" :closable="false" show-icon
        :style="{ marginBottom: '15px', marginTop: '18px' }" />
      <div>
        <!-- 表格 -->
        <template>
          <el-table :data="tableData" style="width: 100%">
            <el-table-column type="index" label="序号" width="100" align="center" >
            </el-table-column>
            <el-table-column prop="title" label="文章标题" width="500" align="center">
              <template v-slot="{ row }">
                <span :style="{ margin: '0 10px' }">{{ row.title }}</span>
                <a href="#" class="el-icon-film" v-if="row.videoURL" @click="videoplayer(row.videoURL)"></a>
              </template>
            </el-table-column>
            <el-table-column prop="visits" label="阅读数" width="100" align="center" >
            </el-table-column>
            <el-table-column prop="username" label="录入人" width="180" align="center" >
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" width="180" align="center" >
              <template v-slot="{ row }">
                <span>{{ new Date(row.createTime) | parseTime }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="180" align="center" >
              <template v-slot="{ row }">
                <span>{{ row.state ? '已启用' : '已禁用' }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="address" label="操作" width="300" align="center" :style="{ fontSize:'16px' }">
              <template v-slot="{ row }">
                <el-button :style="{ fontSize:'14px'}" type="text" size="small" @click="openPreview(row)">预览</el-button>
                <el-button :style="{ fontSize:'14px'}" type="text" size="small" @click="preview(row)">{{
                  row.state ? '禁用' : '启用'
                }}</el-button>
                <el-button :style="{ fontSize:'14px'}" type="text" size="small" :disabled="!!row.state" @click="modify(row)">修改</el-button>
                <el-button :style="{ fontSize:'14px'}" type="text" size="small" @click="del(row.id)" :disabled="!!row.state">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </div>
      <!-- 分页功能 -->
      <el-pagination background layout="prev, pager, next" :current-page="per.page" :page-size="per.pagesize"
        :total="total" @current-change="handleCurrentChange" :style="{ display: 'flex', justifyContent: 'end' ,marginRight:'150px',marginTop:'20px'}" />

    </div>
    <!-- 弹框组件 -->
    <ArticlesAdd ref="openT" @updateList="getList"></ArticlesAdd>
    <!-- 预览弹框组件 -->
    <ArticlesPreview ref="openPreview" @updateList="getList"></ArticlesPreview>
    <div v-if="showVideo" class="video-preview">
      <div>
        <div class="box">
          <video :src="videoURL" controls="controls" autoplay=" autoplay" loop="loop">
          </video>
        </div>
        <div class="close el-icon-close" @click="showVideo = false"> </div>
      </div>

    </div>
  </div>
</template>

<script>
import { list, remove, changeState } from '../../api/hmmm/articles'
import ArticlesAdd from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'
export default {
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      tableData: [],
      showVideo: false,
      per: {
        page: 0,
        pagesize: 10,
        keyword: ''

      },
      total: 0
    }
  },
  created () {
    this.getList()
  },
  methods: {

    // 获取数据列表,渲染页面
    async getList () {
      const res = await list(this.per)
      console.log(res)
      this.tableData = res.data.items
      this.total = res.data.counts

      // 模糊搜索判断
      if (this.tableData.keyword) this.per.keyword = this.tableData.keyword
      if (this.tableData.state === 0 || this.tableData.state === 1) {
        this.per.state = this.tableData.state
      }
    },
    clear () {
      this.per = {
        page: 0,
        pagesize: 10,
        keyword: ''
      }
      this.getList()
    },
    // 打开添加弹框
    unpack () {
      this.$refs.openT.open()
    },
    modify (row) {
      this.$refs.openT.open(row)
    },
    // 分页控制显示页数
    handleCurrentChange (page) {
      console.log(page)
      this.per.page = page
      this.getList()
    },
    // 删除功能
    async del (id) {
      this.$confirm('确认删除吗?')
        .then(async () => {
          await remove({ id })
          this.getList()
        }).then(() => {
          this.$message.success('删除成功')
        }).catch(() => {

        })
    },
    // 禁用和启用
    async preview (row) {
      const state = row.state ? 0 : 1
      try {
        await changeState({ id: row.id, state })
        this.getList()
        this.$message.success('操作成功')
      } catch (error) {
        this.$message.error('操作失败')
      }
    },
    // 预览
    openPreview (id) {
      console.log(id)
      this.$refs.openPreview.openDialogVisible(id)
    },
    // 播放视频
    videoplayer (url) {
      this.videoURL = url
      this.showVideo = true
    }
  }
}
</script>

<style scoped lang='less'>
.el-table{
  font-size: 16px;
}
.container {
  padding: 10px;

}

.body {
  padding: 20px;
  background-color: #fff;
}

.head {
  display: flex;
  flex-direction: row;
  position: relative;
}

.keywords {
  width: 300px;
  height: 40px;
  padding: 0 15px;
}

.depressed {
  background-color: #67c23a;
  position: absolute;
  right: 0;
  top: 0;
}

.shuju {
  padding: 8px 16px;
  color: #909399;
  background-color: #f4f4f5;
  margin: 10px 0;
}

.xuanran {
  display: inline-block;
  height: 50px;
}

.el-icon-film {
  color: #2918ff;
  // font-size: 18px;
}

.video-preview {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .3);
  overflow: hidden;
  z-index: 9999
}

.close {
  width: 50px;
  height: 50px;
  position: absolute;
  top: 10%;
  left: 50%;
  transform: translate(-50%);
  background: rgba(0, 0, 0, .4);
  box-shadow: 0 0 5px rgb(0 0 0 / 40%);
  border-radius: 50%;
  text-align: center;
  line-height: 50px;
  color: #fff;
  font-size: 20px;
}

.box {
  width: 800px;
  height: 600px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.box video {
  width: 100%;
  height: 100%;
}
</style>
